#!perl
package Worker;
use strict;
use warnings;
use HTTP::Request;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windws NT 5.1)');

my $workerNo : shared = 0;
sub new {
	my $nu;
	{
		lock $workerNo;
		$nu = ++ $workerNo;
	}
	return bless {no => $nu}, shift;
}

sub do {
	my ($self, $task) = @_;
	
	#sleep rand(5);
	(my $temp = $task) =~ s/\|status:(.*)$//i;
	print "Worker.", $self->{no}, " do task $temp\n";
	my $response = $ua->get($temp);
	return 0 unless $response->is_success;
	my @links = $response->content =~ /href\s*=\s*(?:\'|\")(.+?)(?:\'|\")/g;
	return {
		result => ["Result for $temp"],
		newTasks => \@links
	};
}

sub fail {
	my ($self, $task) = @_;
	(my $temp = $task) =~ s/status:(.*)$/status:fail/i;
	return $temp;
}

sub success {
	my ($self, $task) = @_;
	(my $temp = $task) =~ s/status:(.*)$/status:complete/i;
	return $temp;
}
1;
